home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr26 / netprog.zip / NETPROG.TAR / lib / timer.c < prev    next >
C/C++ Source or Header  |  1989-12-17  |  3KB  |  162 lines

  1. /*
  2.  * Timer routines.
  3.  *
  4.  * These routines are structured so that there are only the following
  5.  * entry points:
  6.  *
  7.  *    void    t_start()    start timer
  8.  *    void    t_stop()    stop timer
  9.  *    double    t_getrtime()    return real (elapsed) time (seconds)
  10.  *    double    t_getutime()    return user time (seconds)
  11.  *    double    t_getstime()    return system time (seconds)
  12.  *
  13.  * Purposely, there are no structures passed back and forth between
  14.  * the caller and these routines, and there are no include files
  15.  * required by the caller.
  16.  */
  17.  
  18. #include    <stdio.h>
  19. #include    "systype.h"
  20.  
  21. #ifdef BSD
  22. #include    <sys/time.h>
  23. #include    <sys/resource.h>
  24. #endif
  25.  
  26. #ifdef SYS5
  27. #include    <sys/types.h>
  28. #include    <sys/times.h>
  29. #include    <sys/param.h>    /* need the definition of HZ */
  30. #define        TICKS    HZ    /* see times(2); usually 60 or 100 */
  31. #endif
  32.  
  33. #ifdef BSD
  34. static    struct timeval        time_start, time_stop; /* for real time */
  35. static    struct rusage        ru_start, ru_stop;     /* for user & sys time */
  36. #endif
  37.  
  38. #ifdef SYS5
  39. static    long            time_start, time_stop;
  40. static    struct tms        tms_start, tms_stop;
  41. long                times();
  42. #endif
  43.  
  44. static    double            start, stop, seconds;
  45.  
  46. /*
  47.  * Start the timer.
  48.  * We don't return anything to the caller, we just store some
  49.  * information for the stop timer routine to access.
  50.  */
  51.  
  52. void
  53. t_start()
  54. {
  55.  
  56. #ifdef BSD
  57.     if (gettimeofday(&time_start, (struct timezone *) 0) < 0)
  58.         err_sys("t_start: gettimeofday() error");
  59.     if (getrusage(RUSAGE_SELF, &ru_start) < 0)
  60.         err_sys("t_start: getrusage() error");
  61. #endif
  62.  
  63. #ifdef SYS5
  64.     if ( (time_start = times(&tms_start)) == -1)
  65.         err_sys("t_start: times() error");
  66. #endif
  67.  
  68. }
  69.  
  70. /*
  71.  * Stop the timer and save the appropriate information.
  72.  */
  73.  
  74. void
  75. t_stop()
  76. {
  77.  
  78. #ifdef BSD
  79.     if (getrusage(RUSAGE_SELF, &ru_stop) < 0)
  80.         err_sys("t_stop: getrusage() error");
  81.     if (gettimeofday(&time_stop, (struct timezone *) 0) < 0)
  82.         err_sys("t_stop: gettimeofday() error");
  83. #endif
  84.  
  85. #ifdef SYS5
  86.     if ( (time_stop = times(&tms_stop)) == -1)
  87.         err_sys("t_stop: times() error");
  88. #endif
  89.  
  90. }
  91.  
  92. /*
  93.  * Return the user time in seconds.
  94.  */
  95.  
  96. double
  97. t_getutime()
  98. {
  99.  
  100. #ifdef BSD
  101.     start = ((double) ru_start.ru_utime.tv_sec) * 1000000.0
  102.                 + ru_start.ru_utime.tv_usec;
  103.     stop = ((double) ru_stop.ru_utime.tv_sec) * 1000000.0
  104.                 + ru_stop.ru_utime.tv_usec;
  105.     seconds = (stop - start) / 1000000.0;
  106. #endif
  107.  
  108. #ifdef SYS5
  109.     seconds = (double) (tms_stop.tms_utime - tms_start.tms_utime) /
  110.                     (double) TICKS;
  111. #endif
  112.  
  113.     return(seconds);
  114. }
  115.  
  116. /*
  117.  * Return the system time in seconds.
  118.  */
  119.  
  120. double
  121. t_getstime()
  122. {
  123.  
  124. #ifdef BSD
  125.     start = ((double) ru_start.ru_stime.tv_sec) * 1000000.0
  126.                 + ru_start.ru_stime.tv_usec;
  127.     stop = ((double) ru_stop.ru_stime.tv_sec) * 1000000.0
  128.                 + ru_stop.ru_stime.tv_usec;
  129.     seconds = (stop - start) / 1000000.0;
  130. #endif
  131.  
  132. #ifdef SYS5
  133.     seconds = (double) (tms_stop.tms_stime - tms_start.tms_stime) /
  134.                     (double) TICKS;
  135. #endif
  136.  
  137.     return(seconds);
  138. }
  139.  
  140. /*
  141.  * Return the real (elapsed) time in seconds.
  142.  */
  143.  
  144. double
  145. t_getrtime()
  146. {
  147.  
  148. #ifdef BSD
  149.     start = ((double) time_start.tv_sec) * 1000000.0
  150.                 + time_start.tv_usec;
  151.     stop = ((double) time_stop.tv_sec) * 1000000.0
  152.                 + time_stop.tv_usec;
  153.     seconds = (stop - start) / 1000000.0;
  154. #endif
  155.  
  156. #ifdef SYS5
  157.     seconds = (double) (time_stop - time_start) / (double) TICKS;
  158. #endif
  159.  
  160.     return(seconds);
  161. }
  162.